package com.songlanyun.common.supplychainapi.order;


import com.jxhh.ApiClient;
import com.songlanyun.common.supplychainapi.common.ApiRequestCommon;
import com.songlanyun.common.supplychainapi.common.ResultCommon;
import com.songlanyun.common.supplychainapi.order.model.*;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class ApiOrderCommon {

    /**
     * 余额不足订单重新补单
     */
    private static final String ERROR_LISTS_URL = "/v2/order/errorLists";
    /**
     * 错误订单详情
     */
    private static final String LIST_ERROR_INFO_URL = "/v2/order/ListErrorInfo";
    /**
     * 订单错误列表【最新】
     */
    private static final String LIST_ERROR_NEW_URL = "/v2/order/ListErrorNew";
    /**
     * 可售检测V2
     */
    private static final String AVAILABLE_CHECK_URL = "/v2/order/availableCheck";
    /**
     * 前置校验V2
     */
    private static final String BEFORE_CHECK_URL = "/v2/order/beforeCheck";
    /**
     * 提交下单V2、全平台订单列表V2
     */
    private static final String ORDER_URL = "/v2/order";
    /**
     * 各平台订单列表V2
     */
    private static final String ORDER_SOURCE_URL = "/v2/order/source";

    @Resource
    private ApiRequestCommon request;

    /**
     * 余额不足订单重新补单
     *
     * @param errId 错误订单序号
     * @return
     */
    public void errorLists(String errId) {
        request.exec(ERROR_LISTS_URL + "/" + errId, ApiClient.RequestMethod.POST, new HashMap<>(), null, "余额不足订单重新补单");
    }

    /**
     * 错误订单详情
     *
     * @param id 订单错误列表最新  接口返回的id
     * @return
     */
    public ListErrorInfo listErrorInfo(String id) {
        return request.exec(LIST_ERROR_INFO_URL + "/" + id, ApiClient.RequestMethod.GET, new HashMap<>(), ListErrorInfo.class, "错误订单详情");
    }

    /**
     * 订单错误列表【最新】
     *
     * @param params
     * @return
     */
    public ListErrorNew listErrorNew(Map<String, Object> params) {
        return request.exec(LIST_ERROR_NEW_URL, ApiClient.RequestMethod.GET, params, ListErrorNew.class, "订单错误列表");
    }

    /**
     * 可售检测V2
     *
     * @return
     */
    public AvailableCheck availableCheck(CheckParam checkParam) {
        Map<String, Object> params = ClassConvertUtils.transformObject(checkParam, Map.class);
        return request.exec(AVAILABLE_CHECK_URL, ApiClient.RequestMethod.GETBODY, params, AvailableCheck.class, "可售检测");
    }

    /**
     * 前置校验V2
     *
     * @return
     */
    public BeforeCheck[] beforeCheck(CheckParam checkParam) {
        Map<String, Object> params = ClassConvertUtils.transformObject(checkParam, Map.class);
        return request.exec(BEFORE_CHECK_URL, ApiClient.RequestMethod.POST, params, BeforeCheck[].class, "前置校验");
    }

    /**
     * 提交下单V2
     *
     * @return
     */
    public void orderSubmit(OrderParam orderParam) {
        Map<String, Object> params = ClassConvertUtils.transformObject(orderParam, Map.class);
        request.exec(ORDER_URL, ApiClient.RequestMethod.POST, params, null, "提交下单");
    }

    /**
     * 全平台订单列表V2
     *
     * @return
     */
    public OrderList orderList(Map<String, Object> params) {
        return request.exec(ORDER_URL, ApiClient.RequestMethod.GET, params, OrderList.class, "全平台订单列表");
    }

    /**
     * 各平台订单列表V2
     *
     * @param source 商品来源：1.云仓2.京东6.阿里7.天猫11.华南一仓14华东一仓15淘宝
     * @return
     */
    public OrderSourceList orderSourceList(Integer source) {
        return request.exec(ORDER_SOURCE_URL + "/" + source, ApiClient.RequestMethod.GET, new HashMap<>(), OrderSourceList.class, "各平台订单列表");
    }

}
